package wpwork.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wpwork.redisEntity.*;
import wpwork.service.RedisPublishService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class DataInteractionService {

    @Autowired
    private RedisPublishService redisPublishService;

    /**
     * 处理人员信息发布
     */
    public boolean handlePersonInfo(String tenantId, List<UserInfo> userList) {
        PersonInfoRequest request = new PersonInfoRequest();
        request.setF_serial(generateSerial());
        request.setF_tenant_id(tenantId);
        request.setF_user_list(userList);
        request.setF_time(getCurrentTime());

        return redisPublishService.publishPersonInfo(request);
    }

    /**
     * 处理位置查询请求
     */
    public boolean handleLocationQuery(String tenantId) {
        LocationRequest request = new LocationRequest();
        request.setF_serial(generateSerial());
        request.setF_tenant_id(tenantId);
        request.setF_time(getCurrentTime());

        return redisPublishService.publishLocationRequest(request);
    }

    /**
     * 处理轨迹查询请求
     */
    public boolean handleTrackQuery(String tenantId, String beginTime, String endTime) {
        TrackRequest request = new TrackRequest();
        request.setF_serial(generateSerial());
        request.setF_tenant_id(tenantId);
        request.setF_begintime(beginTime);
        request.setF_endtime(endTime);
        request.setF_time(getCurrentTime());

        return redisPublishService.publishTrackRequest(request);
    }

    /**
     * 处理AI外呼请求
     */
    public boolean handleAICall(String tenantId, String message, List<UserInfo> userList) {
        AICallRequest request = new AICallRequest();
        request.setF_serial(generateSerial());
        request.setF_tenant_id(tenantId);
        request.setF_message(message);
        request.setF_user_list(userList);
        request.setF_time(getCurrentTime());

        return redisPublishService.publishAICallRequest(request);
    }

    private String generateSerial() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    private String getCurrentTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}